import * as type from "./actionType"


let allEmoji = ["😄", "😃", "😁", "😆", "😅", "🤣", "😂", "🙂", "🙃", "😉", "😊", "😇", "😍", "🥰", "🤩", "😘", "😗", "😚", "😙", "😛", "😋", "😜", "🤪", "😝", "🤑", "🤔", "🤗", "🤭", "🤫", "🤐", "🤨", "😐", "😑", "😶", "😏", "😒", "🙄", "😬", "🤥", "😴", "😌", "😔", "😪", "🤤", "🤧", "😷", "🤒", "🤕", "🤢", "🤮", "🥵", "🥶", "🥴", "😵", "🤯", "🤠", "🥳", "😎", "🤓", "🧐"]
const random = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1)) + min
}
const createGameList = () => {
    let set = new Set()
    let resultArr, chaos

    while (set.size < 15) {
        set.add(random(0, allEmoji.length - 1))
    }//生成不重样的15个emoji

    resultArr = [...set]//set转数组
    resultArr.push(resultArr[resultArr.length - 1])//把最后一个取出作为第16个
    chaos = resultArr.map((v, i) => {
        return allEmoji[v]                   //把下标值 转换为 对应的 emoji
    }).sort(() => {
        return Math.random() - 0.5           //emoji数组乱序
    })

    return chaos
}
const extra_time = 5 * 1000 //奖励时间 5 秒



const reducer = (state, action) => {
    // let newState = JSON.parse(JSON.stringify(state)) 这种方法有bug
    let newState = { ...state }
    switch (action.type) {
        case type.create_topic:
            // 创建题目同时把activeEmoji activeIndex correctIndex 初始化
            newState.activeEmoji = ''
            newState.activeIndex = -1
            newState.correctIndex = -1
            newState.emojis = createGameList()
            return newState
        case type.found_one:
            newState.found += 1
            return newState
        case type.set_game_state:
            newState.gameStart = action.value
            return newState
        case type.set_active_emoji:
            newState.activeEmoji = action.value
            return newState
        case type.set_active_index:
            newState.activeIndex = action.value
            return newState
        case type.set_correct_index:
            newState.correctIndex = action.value
            return newState
        case type.gain_time:
            action.value ? newState.time += action.value : 
            newState.time += extra_time
            return newState
        case type.reduce_time:
                newState.time -= 1000
            return newState
        case type.reset_emojis:
            newState.emojis = []
            return newState
        case type.set_playEnd:
            newState.playEnd = action.value
            return newState
        case type.found_reset:
            newState.found = 0
            return newState
        default:
            return state
    }
}
export default reducer